# Maintainer: Michael Hansen <zrax0111 gmail com>

_realname=unicorn
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-unicorn"
         "${MINGW_PACKAGE_PREFIX}-python-unicorn")
pkgver=2.0.1.post1
pkgrel=3
pkgdesc="A lightweight multi-platform, multi-architecture CPU emulator framework based on QEMU (mingw-w64)"
url='https://www.unicorn-engine.org/index.html'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
license=('GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/unicorn-engine/unicorn/archive/${pkgver}.tar.gz"
        "001-bindings-python.patch"
        "002-getpagesize-clang.patch")
sha512sums=('8694d6bc92e3424a8ad050316413d53e56e0f55e7cad7517fb3e98e670a0f1768b060ead8f195da13607cec89a964364f05a8b9d0dc074f4ac5e51026f8343ad'
            '966ba5eeae1e72c400fc7f6a7f3a9b14afb9b0abf02c53e1ca18b7cad9e84705b8fd86fe8f42fd5d59a4bab312986952cc07119f4b1d70d4973850b16f5e1aa9'
            'ba35ee611ee312c851a07533caaf8a7a6c6a398fe3544c9ba843a2e3c333859ef0433fbbee28ff0aaa61893f44cea45691339fcc05779a3837ae5fdbdd87577a')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  pwd

  patch -Np1 -i "${srcdir}"/001-bindings-python.patch
  patch -Np1 -i "${srcdir}"/002-getpagesize-clang.patch
}

build() {
  cd "${srcdir}"/${_realname}-${pkgver}

  # Build unicorn
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -GNinja \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .

  # Build bindings
  (cd "${srcdir}"/${_realname}-${pkgver}/bindings
    ${MINGW_PREFIX}/bin/python const_generator.py python
  )
  (cd "${srcdir}"/${_realname}-${pkgver}/bindings/python
    ${MINGW_PREFIX}/bin/python setup.py build
  )
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build . --target test
}

package_unicorn() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.LGPL2" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LGPL2"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING_GLIB" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING_GLIB"
}

package_python-unicorn() {
  depends=("${MINGW_PACKAGE_PREFIX}-python" "${MINGW_PACKAGE_PREFIX}-unicorn")
  provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")

  cd "${_realname}-${pkgver}/bindings/python"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --root="${pkgdir}" --prefix="${MINGW_PREFIX}" \
    -O1 --skip-build

  install -Dm 644 sample* shellcode.py -t "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}/samples"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
